package model;

import java.awt.geom.Point2D;
import java.math.BigDecimal;
import java.math.BigInteger;
import java.math.RoundingMode;
import java.util.Vector;

public class HCalc {

	public final static int DECIMAL_PLACES = 8;
    // N = population size
    // K = number of successes
    // n = number of draws
    // k = number of drawn successes w/o replacement
	
	public static HCalcResults calculateProbFromRange(Point2D.Double x, int N, int n, int k) {
		Vector<Point2D.Double> points = new Vector<Point2D.Double>();
		
		Point2D.Double point; 
		for(int i = (int) x.getX(); i <= x.getY(); i++){
			point = new Point2D.Double();
			point.setLocation(i, calculateProb(i, N, n, k));
			points.add(point);
		}
		
		return new HCalcResults(points, N, n, k);
	}
	
	private static double calculateProb(int x, int N, int n, int k) {
		double d = ((choose(k, x).multiply(choose(N-k, n-x))).divide(choose(N, n),DECIMAL_PLACES, RoundingMode.HALF_EVEN)).doubleValue();
		return d;
	}
	
	private static BigDecimal choose(int n, int k) {
		BigInteger num = BigInteger.valueOf(1); 
		BigInteger den = BigInteger.valueOf(1);
	
		for (int i = n - k + 1; i <= n; i++)
			num = num.multiply(BigInteger.valueOf(i));
		
		for (int i = 2; i <= k; i++)
			den = den.multiply(BigInteger.valueOf(i));
		
		return new BigDecimal(num.divide(den));
	}
}
